#############################################
#Covariate balance between treated voters with VCSF update and those without
#############################################

rm(list = ls())

#Functions
source("functions.R")

#Packages
installPackageNotFound("xtable")
installPackageNotFound("lfe")

#############################################
#Read in Voter data
#############################################

#All voters
all.voters <- read.csv("voters.csv", stringsAsFactors = F)

#Drop voters not in same household
all.voters <- all.voters[which(all.voters$same_household == 0), ]

#############################################
#Statistical tests for methods of contact
#############################################

#Flag to treated voters with any update
all.voters$VCSFResponse <- ifelse(all.voters$Treatment == 1 &
                                     !is.na(all.voters$total_updates), 1, 0)

#Voter controls
covariates <- c("Black", "Hispanic", "Asian", "Male", "Under30",
                "VotePropensity", "Partisanship")

#Data groups
data.list <- list("in.district" = which(all.voters$Volunteer_In_District == 1),
                  "out.district" = which(all.voters$Volunteer_In_District == 0))

#Object to hold results
result.df <- c()

for(k in 1:length(data.list)){

  #Select data
  test.data <- all.voters[data.list[[k]],]

  #Run tests
  this.result <-runTTests(test.data = test.data,
                          outcome.var.names = covariates,
                          indp.var = "VCSFResponse")

  #Label sample
  this.result$sample <- names(data.list)[k]

  #Add to results
  result.df <- rbind(result.df,this.result)
}

#Separate means and ses
mean.df <- result.df[,c("sample", "outcome", "control_mean",
                        "treatment_mean", "bh_p_value")]

#Format
mean.df$control_mean <- formatSig(mean.df$control_mean,2)
mean.df$treatment_mean <- formatSig(mean.df$treatment_mean,2)
mean.df$bh_p_value <- formatSig(mean.df$bh_p_value,2)
mean.df$type <- 1

#Insert ses underneath means
se.df <- result.df[,c("sample", "outcome", "control_se",
                      "treatment_se", "bh_p_value")]

#Format
se.df$control_se <- paste0("(",formatSig(se.df$control_se,2), ")")
se.df$treatment_se <- paste0("(", formatSig(se.df$treatment_se,2), ")")
se.df$bh_p_value <- ""
se.df$type <- 2

#Rename
names(se.df) <- names(mean.df)

#Bind together
out.table <- rbind(mean.df, se.df)

#Sort
out.table <- out.table[order(out.table$sample, out.table$outcome),]

#Blank out the variable
out.table$outcome[which(out.table$type==2)] <- ""

#Widen
out.table.wide <- cbind(out.table[which(out.table$sample=="in.district"),
                                  c("outcome", "control_mean", "treatment_mean",
                                    "bh_p_value")],
                        out.table[which(out.table$sample=="out.district"),
                                  c("control_mean", "treatment_mean",
                                    "bh_p_value")])

#Remove row names
row.names(out.table.wide) <- c()

print(out.table.wide)
